package com.xiangyueEducation.uploaderCloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyueEducation.uploaderCloud.mapper.DoctorMapper;
import com.xiangyueEducation.uploaderCloud.pojo.dto.PageQuery;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Doctor;
import com.xiangyueEducation.uploaderCloud.pojo.vo.PageVo;
import com.xiangyueEducation.uploaderCloud.service.DoctorService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {
    @Resource
    private DoctorMapper doctorMapper;
    @Override
    public PageVo pageQuery(PageQuery pageQuery, String keyword) {
        Integer pageSize = pageQuery.getPageSize();
        Integer pageNo = pageQuery.getPageNo();
        //添加分页基本参数
        Page<Doctor> doctorPage = new Page<>(pageNo, pageSize);
        //设置排序参数，按照时间顺序，升序
        OrderItem orderItem1 = new OrderItem().setAsc(true).setColumn("rating");
        doctorPage.addOrder(orderItem1);
        Page<Doctor> page = null;
        if (keyword!=null){
            //去数据库查
            page = page(doctorPage,new QueryWrapper<Doctor>().like("doctor_name", keyword));
        }
        else{
            page = page(doctorPage,new QueryWrapper<Doctor>());
        }
        List<Doctor> records = page.getRecords();
        long total = page.getTotal();
        return new PageVo(total,records);
    }

    @Override
    public void update(Doctor doctor) {
        doctorMapper.updateAll(doctor);
    }

    @Override
    public Doctor selectByUserId(Long doctorUserId) {
        return doctorMapper.selectOne(new QueryWrapper<Doctor>().eq("user_id", doctorUserId));
    }
}
